
var config = require('@/config/config.js');
export default {
	httpGet:function(uri,param,successCallback, errorCallback,isShowLoading = true){
		return new Promise((resolve, reject) => {
			uni.request({
				url: config.url+uri,
				data:param,
				cache:false,
				method:'GET',
				timeout:1000,
				header: {
					'Content-Type': 'application/x-www-form-urlencoded',
				},
				success:(res) => {
					if (isShowLoading) {
						uni.hideLoading(); //关闭加载动画
					}
					if (res.statusCode == 401) {
						uni.showToast({
							icon: 'none',
							title: '认证已过期，请重新登录。',
							success: function() {
								setTimeout(function() {
									uni.reLaunch({
										url: '/pages/login/login'
									});
								}, 1000);
							}
						});
						return;
					}
					
					if(res.data.bizSucc===false&&isShowLoading == false){
						uni.showToast({
							title: res.data.errMsg,
							icon:'none',
							duration: 3000
						});	
						return;
					}
					if (successCallback != null && typeof(successCallback) == 'function') {
						successCallback(res);
					}	
					resolve(res.data.respData);
				},
				fail:(err) => {  
					resolve(err);
					if (isShowLoading) {
						uni.hideLoading(); //关闭加载动画
					}
					if (errorCallback != null && typeof(errorCallback) == 'function') {
						errorCallback(err);
					}
					else{
						uni.showToast({
							title: '加载失败!',
							icon:'none',
							duration: 3000
						});					
					}
				}
			})
		})
	},
	httpPost:function(uri,param,successCallback, errorCallback,isShowLoading = true){
		return new Promise((resolve, reject) => {
			uni.request({
				url: config.url+uri,
				data:param,
				method:'POST',
				timeout:1000,
				header: {
					'Content-Type': 'application/json'
				},
				success:(res) => {
					if (isShowLoading) {
						uni.hideLoading(); //关闭加载动画
					}
					if (res.statusCode == 401) {
						uni.showToast({
							icon: 'none',
							title: '认证已过期，请重新登录。',
							success: function() {
								setTimeout(function() {
									uni.reLaunch({
										url: '/pages/login/login'
									});
								}, 1000);
							}
						});
						return;
					}
					if (successCallback != null && typeof(successCallback) == 'function') {
						successCallback(res);
					}				
				},
				fail:(err) => {  
					console.log(err)
					if (isShowLoading) {
						uni.hideLoading(); //关闭加载动画
					}
					if (errorCallback != null && typeof(errorCallback) == 'function') {
						errorCallback(err);
					}
					else{
						uni.showToast({
							title: '加载失败!',
							icon:'none',
							duration: 3000
						});					
					}
				} 
				
			})
		})
		
	},
	
}
